package view;


import controller.StudentController;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
import javax.swing.JLabel;
import javax.swing.Timer;

/**
 *
 * @author NOORANI
 */


//timerLabel, extends JLabel. Implements Runnable to allow to run it in a new thread
public class TimerLabel extends JLabel implements Runnable {

    private long millis;
    private Timer timer;
    StudentController studentController;
    
    //constructor, receives the time for the timer in MILLISECONDS
    public TimerLabel(long millis,StudentController studentController) {
        super(String.valueOf(millis));
        this.studentController=studentController;
        this.millis = millis;
        setAlignmentX(Component.CENTER_ALIGNMENT);
    }

    @Override
    public void setText(String text) {
        
        //setText method (overrided from the jlabel
        //converts the millis variable (time left) into a string in the format of HH:MM:SS

        if (text != null && text.length() > 0) {
            long millis = Long.parseLong(text);
            if (millis > -1000) {
                super.setText(String.format("%02d:%02d:%02d",
                        TimeUnit.MILLISECONDS.toHours(millis),
                        TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                        TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
            } else {
                System.exit(0);
            }
        }
    }

    public void startTimer() {
        
        //starts the timmer
        
        if (timer == null) {
            timer = new Timer(100, new ActionListener() { //(I replace, original 1000)// creates a timer which fires an event every when the especified time in the constructor passes. In this case it will fire the event every 0.1 seconds

                @Override
                public void actionPerformed(ActionEvent e) {
                    decrement();    //call the decrement function every 0.1s
                }
            });
        }
        timer.start();  //start the timer created
    }

    public void stopTimer() {
        timer.stop();   //stop the timer created
    }

    public void decrement() {
       
        //function called every 0.1s
        
        millis -= 100;//(original=1000)//decrement total time left in 100 milliseconds (0.1s)
        
        //check if time for the student has expired
        if (millis<=0){
            studentController.timeExpired();
            timer.stop();
        }
        
        //update the label txt
        setText(String.valueOf(millis));
        
        
        //steps to make the timmer blink
        if (millis>10000 && millis<=30000){
            if ((millis/1000) % 2 == 0){
                this.setOpaque(true);
                this.setForeground(Color.black);
            }
            else{
                this.setOpaque(true);
                this.setForeground(Color.red);
            }
        }
        else if (millis <=10000){
            if ((millis/500) % 2 == 0){
                this.setOpaque(true);
                this.setForeground(Color.black);
            }
            else{
                this.setOpaque(true);
                this.setForeground(Color.red);
            }
        }
        

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        startTimer();
    }
}